let apiPath = 'https://dfdb.dev.znxkeji.cn/api'
// let apiPath = 'http://192.168.1.3:13333'
let imgPath = 'https://dfdb.dev.znxkeji.cn/api'
let url = 'https://dfdb.dev.znxkeji.cn'

let accessToken = 'db_accessToken'
let refreshToken = 'db_refreshToken'
let aut = null
const that = this

function request(method, url, data, func) {
  uni.getStorage({
    key: accessToken,
    success: function(re) {
      aut = re.data
      // console.log("请求", aut);
      uni.request({
        url: apiPath + url,
        method: method,
        header: {
          // 'Content-Type': 'application/x-www-form-urlencoded',
          'Authorization': `Basic c2FiZXI6c2FiZXJfc2VjcmV0`,
          'User-Type': uni.getStorageSync('userType'),
          "Blade-Auth": 'bearer ' + aut,
        },
        data: data,
        success(res) {
          // console.log(res);
          if (res && res.data.code == 200) {
            return typeof func == "function" && func(res);
          } else if (res.data.code == 401) {
            requests(method, url, data, func)
          } else {
            return typeof func == "function" && func(res);
          }
        },
        fail(err) {

          return typeof func == "function" && func(res);
        }
      })
    },
    fail(err) {
      uni.request({
        url: apiPath + url,
        method: method,
        header: {
          // 'Content-Type': 'application/x-www-form-urlencoded',
          'Authorization': `Basic c2FiZXI6c2FiZXJfc2VjcmV0`,
          'User-Type': uni.getStorageSync('userType'),
          "Blade-Auth": 'bearer ' + aut,
          "user-device": uni.getStorageSync('user-device')
        },
        data: data,
        success(res) {
          if (res && res.data.code == 200) {
            return typeof func == "function" && func(res);
          } else if (res.data.code == 401) {
            console.log("成功");
            requests(method, url, data, func)
          } else {
            return typeof func == "function" && func(res);
          }
        },
        fail(err) {
          return typeof func == "function" && func(err);
        }
      })
    }
  })

}

function uploadFile(data, func) {
  uni.getStorage({
    key: accessToken,
    success: function(re) {
      aut = re.data
      // console.log("请求", aut);
      uni.uploadFile({
        url: apiPath + '/blade-resource/oss/endpoint/put-file',
        header: {
          // 'Content-Type': 'application/x-www-form-urlencoded',
          'Authorization': `Basic c2FiZXI6c2FiZXJfc2VjcmV0`,
          'User-Type': uni.getStorageSync('userType'),
          "Blade-Auth": 'bearer ' + aut,
          "user-device": uni.getStorageSync('user-device')
        },
        filePath: data,
        name: 'file',
        success(res) {
          return typeof func == "function" && func(res);
        },
        fail(err) {
          return typeof func == "function" && func(res);
        }
      })
    },
    fail(res) {
      uni.showToast({
        title: `上传失败`,
        icon: 'none'
      })
    }
  })

}

function requests(method, url, data, func) {
  uni.getStorage({
    key: refreshToken,
    success: function(re) {
      aut = re.data
      uni.request({
        url: apiPath + `/blade-auth/token?grantType=refresh_token&refreshToken=${re.data}`,
        method: "POST",
        header: {
          // 'Content-Type': 'application/x-www-form-urlencoded',
          'Authorization': `Basic c2FiZXI6c2FiZXJfc2VjcmV0`,
          'User-Type': uni.getStorageSync('userType'),
        },
        success(res) {
          if (res.data.code == 200) {
            uni.setStorage({
              key: accessToken,
              data: res.data.data.accessToken
            })
            uni.setStorage({
              key: refreshToken,
              data: res.data.data.refreshToken
            })
            return request(method, url, data, func)
          } else {
            uni.removeStorageSync(accessToken);
            uni.removeStorageSync(refreshToken);
            uni.reLaunch({
              url: '/pages/index/index'
            })
          }
        },
        fail(err) {
          uni.removeStorageSync(accessToken);
          uni.removeStorageSync(refreshToken);
          uni.reLaunch({
            url: '/pages/index/index'
          })
        }
      })
    },
    fail() {
      uni.removeStorageSync(accessToken);
      uni.removeStorageSync(refreshToken);
      uni.reLaunch({
        url: '/pages/index/index'
      })
    }
  })

}

function isJSON(str) {
  if (typeof str == 'string') {
    try {
      var obj = JSON.parse(str);
      if (typeof obj == 'object' && obj) {
        return true;
      } else {
        return false;
      }

    } catch (e) {
      // console.log('error：' + str + '!!!' + e);
      return false;
    }
  } else
    return true;
  // console.log('It is not a string!')
}


export default {
  url,
  imgPath,
  accessToken,
  refreshToken,
  get(url, data, func) {
    request('GET', url, data, function(res) {
      return typeof func == "function" && func(res)
    });
  },
  post(url, data, func) {
    request('POST', url, data, function(res) {
      return typeof func == "function" && func(res)
    });
  },
  to(url) {
    uni.navigateTo({
      url: url
    })
  },
  upload(data, func) {
    uploadFile(data, (res) => {
      return typeof func == "function" && func(res)
    })
  },
  showToast(data) {
    uni.showToast({
      title: data,
      icon: 'none'
    })
  },
  setClipboardData(data) {
    uni.setClipboardData({
      data: data,
      success: function() {
        uni.showToast({
          title: "复制成功",
          icon: 'none'
        })
      }
    });
  }

}